package com.cccyy.data.jsondata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class JsonDataGetWithKey {
    public static void main(String[] args) throws Exception {
        String filePathGroup = "D:\\WorkSpace\\Study\\study-test\\src\\main\\java\\com\\cccyy\\data\\jsondata\\JsonText_groups.txt";
        String filePathApi = "D:\\WorkSpace\\Study\\study-test\\src\\main\\java\\com\\cccyy\\data\\jsondata\\JsonText_apiList.txt";
        List<ApiInfo> apiInfos = getJsonDataApi(filePathApi);
        List<GroupInfo> groupInfos = getJsonDataGroup(filePathGroup);
        //取groupInfos中 seq、name 组成map<String,String>
        Map<Integer, String> groupMap = groupInfos.stream()
                .collect(Collectors.toMap(GroupInfo::getSeq, GroupInfo::getName));

//        HashMap<String, String> nameMap = new HashMap<>();
        ArrayList<String> names = new ArrayList<>();
        for (ApiInfo apiInfo : apiInfos) {
            int parentSeq = apiInfo.getParentSeq();
            if (groupMap.containsKey(parentSeq)) {
                names.add(apiInfo.getName());
//                nameMap.put(groupMap.get(parentSeq), apiInfo.getName());
                System.out.println(groupMap.get(parentSeq));
//                System.out.println(groupMap.get(parentSeq) + "|" + apiInfo.getName());
            } else {
                System.out.println("跳过的参数有：" + apiInfo.getApiCode() + "  " + apiInfo.getName());
            }
        }
        //先打印出nameMap的key 再打印出 value
//        for (String key : nameMap.keySet()) {
//            System.out.println(key);
//        }
        System.out.println("间隔行：=============================================================");
//        for (String value : nameMap.values()) {
//            System.out.println(value);
//        }
        for (String name : names) {
            System.out.println(name);
        }
    }

    public static List<ApiInfo> getJsonDataApi(String filePath) throws Exception {
        String jsonData = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
        JSONObject jsonObject = JSON.parseObject(jsonData); // 将 JSON 字符串解析为 JSONObject
        JSONArray interBriefsArray = jsonObject.getJSONArray("interBriefs"); // 获取 JSONArray
        return interBriefsArray.toJavaList(ApiInfo.class); // 转换为 ApiInfo 列表

    }

    public static List<GroupInfo> getJsonDataGroup(String filePath) throws Exception {
        String jsonData = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
        JSONObject jsonObject = JSON.parseObject(jsonData); // 将 JSON 字符串解析为 JSONObject
        JSONArray groupsArray = jsonObject.getJSONArray("groups"); // 获取 JSONArray
        return groupsArray.toJavaList(GroupInfo.class); // 转换为 GroupInfo 列表
    }
}
